<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>cbc_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_crypt_cbc_test.js.html">cbc_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2012 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Unit tests for CBC mode for block ciphers.
<a name="line17"></a> *
<a name="line18"></a> * @author nnaze@google.com (Nathan Naze)
<a name="line19"></a> */
<a name="line20"></a>
<a name="line21"></a>goog.provide(&#39;goog.crypt.CbcTest&#39;);
<a name="line22"></a>
<a name="line23"></a>goog.require(&#39;goog.crypt.Aes&#39;);
<a name="line24"></a>goog.require(&#39;goog.crypt.Cbc&#39;);
<a name="line25"></a>goog.require(&#39;goog.crypt&#39;);
<a name="line26"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line27"></a>
<a name="line28"></a>goog.setTestOnly(&#39;goog.crypt.CbcTest&#39;);
<a name="line29"></a>
<a name="line30"></a>function stringToBytes(s) {
<a name="line31"></a>  var bytes = new Array(s.length);
<a name="line32"></a>  for (var i = 0; i &lt; s.length; ++i)
<a name="line33"></a>    bytes[i] = s.charCodeAt(i) &amp; 255;
<a name="line34"></a>  return bytes;
<a name="line35"></a>}
<a name="line36"></a>
<a name="line37"></a>function runCbcAesTest(keyBytes, initialVectorBytes, plainTextBytes,
<a name="line38"></a>                       cipherTextBytes) {
<a name="line39"></a>
<a name="line40"></a>  var aes = new goog.crypt.Aes(keyBytes);
<a name="line41"></a>  var cbc = new goog.crypt.Cbc(aes);
<a name="line42"></a>
<a name="line43"></a>  var encryptedBytes = cbc.encrypt(plainTextBytes, initialVectorBytes);
<a name="line44"></a>  assertEquals(&#39;Encrypted bytes should match cipher text.&#39;,
<a name="line45"></a>               goog.crypt.byteArrayToHex(cipherTextBytes),
<a name="line46"></a>               goog.crypt.byteArrayToHex(encryptedBytes));
<a name="line47"></a>
<a name="line48"></a>  var decryptedBytes = cbc.decrypt(cipherTextBytes, initialVectorBytes);
<a name="line49"></a>  assertEquals(&#39;Decrypted bytes should match plain text.&#39;,
<a name="line50"></a>               goog.crypt.byteArrayToHex(plainTextBytes),
<a name="line51"></a>               goog.crypt.byteArrayToHex(decryptedBytes));
<a name="line52"></a>}
<a name="line53"></a>
<a name="line54"></a>function testAesCbcCipherAlgorithm() {
<a name="line55"></a>  // Test values from http://www.ietf.org/rfc/rfc3602.txt
<a name="line56"></a>
<a name="line57"></a>  // Case #1
<a name="line58"></a>  runCbcAesTest(
<a name="line59"></a>      goog.crypt.hexToByteArray(&#39;06a9214036b8a15b512e03d534120006&#39;),
<a name="line60"></a>      goog.crypt.hexToByteArray(&#39;3dafba429d9eb430b422da802c9fac41&#39;),
<a name="line61"></a>      stringToBytes(&#39;Single block msg&#39;),
<a name="line62"></a>      goog.crypt.hexToByteArray(&#39;e353779c1079aeb82708942dbe77181a&#39;));
<a name="line63"></a>
<a name="line64"></a>  // Case #2
<a name="line65"></a>  runCbcAesTest(
<a name="line66"></a>      goog.crypt.hexToByteArray(&#39;c286696d887c9aa0611bbb3e2025a45a&#39;),
<a name="line67"></a>      goog.crypt.hexToByteArray(&#39;562e17996d093d28ddb3ba695a2e6f58&#39;),
<a name="line68"></a>      goog.crypt.hexToByteArray(
<a name="line69"></a>          &#39;000102030405060708090a0b0c0d0e0f&#39; +
<a name="line70"></a>          &#39;101112131415161718191a1b1c1d1e1f&#39;),
<a name="line71"></a>      goog.crypt.hexToByteArray(
<a name="line72"></a>          &#39;d296cd94c2cccf8a3a863028b5e1dc0a&#39; +
<a name="line73"></a>          &#39;7586602d253cfff91b8266bea6d61ab1&#39;));
<a name="line74"></a>
<a name="line75"></a>  // Case #3
<a name="line76"></a>  runCbcAesTest(
<a name="line77"></a>      goog.crypt.hexToByteArray(&#39;6c3ea0477630ce21a2ce334aa746c2cd&#39;),
<a name="line78"></a>      goog.crypt.hexToByteArray(&#39;c782dc4c098c66cbd9cd27d825682c81&#39;),
<a name="line79"></a>      stringToBytes(&#39;This is a 48-byte message (exactly 3 AES blocks)&#39;),
<a name="line80"></a>      goog.crypt.hexToByteArray(
<a name="line81"></a>          &#39;d0a02b3836451753d493665d33f0e886&#39; +
<a name="line82"></a>          &#39;2dea54cdb293abc7506939276772f8d5&#39; +
<a name="line83"></a>          &#39;021c19216bad525c8579695d83ba2684&#39;));
<a name="line84"></a>
<a name="line85"></a>  // Case #4
<a name="line86"></a>  runCbcAesTest(
<a name="line87"></a>      goog.crypt.hexToByteArray(&#39;56e47a38c5598974bc46903dba290349&#39;),
<a name="line88"></a>      goog.crypt.hexToByteArray(&#39;8ce82eefbea0da3c44699ed7db51b7d9&#39;),
<a name="line89"></a>      goog.crypt.hexToByteArray(
<a name="line90"></a>          &#39;a0a1a2a3a4a5a6a7a8a9aaabacadaeaf&#39; +
<a name="line91"></a>          &#39;b0b1b2b3b4b5b6b7b8b9babbbcbdbebf&#39; +
<a name="line92"></a>          &#39;c0c1c2c3c4c5c6c7c8c9cacbcccdcecf&#39; +
<a name="line93"></a>          &#39;d0d1d2d3d4d5d6d7d8d9dadbdcdddedf&#39;),
<a name="line94"></a>      goog.crypt.hexToByteArray(
<a name="line95"></a>          &#39;c30e32ffedc0774e6aff6af0869f71aa&#39; +
<a name="line96"></a>          &#39;0f3af07a9a31a9c684db207eb0ef8e4e&#39; +
<a name="line97"></a>          &#39;35907aa632c3ffdf868bb7b29d3d46ad&#39; +
<a name="line98"></a>          &#39;83ce9f9a102ee99d49a53e87f4c3da55&#39;));
<a name="line99"></a>}
</pre>


</body>
</html>
